package com.cn.web.controller.PartsManagement;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.dto.BusinessProjectParameterItemMappingDto;
import com.cn.bean.dto.SystemParametersDto;
import com.cn.bean.pojo.PartsManagement.ComponentItemBasicInfoTable;
import com.cn.bean.pojo.lbj.ParameterItemTable;
import com.cn.bean.pojo.sys.SystemParameterSub_Table;
import com.cn.bean.pojo.sys.SystemParametersMainTable;
import com.cn.bean.pojo.yw.*;
import com.cn.bean.vo.PartsManagement.ComponentItemClassification;
import com.cn.config.config.Result;
import com.cn.service.server.PartsManagement.BusinessProjectParameterItemMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Slf4j
@Transactional
public class BusinessProjectParameterItemMappingController {
    @Autowired
    private BusinessProjectParameterItemMappingService parameterItemMappingService;


    @RequestMapping("/select/BusinessProjectParameterItemMappingMain")
    @ResponseBody
    public List<BusinessProjectParameterItemMappingMain> selectBusinessProjectParameterItemMappingMain(@RequestBody BusinessProjectParameterItemMappingMain mappingMain) {
        List<BusinessProjectParameterItemMappingMain> assists = parameterItemMappingService.selectBusinessProjectParameterItemMappingMain(mappingMain);
        return assists;
    }
    /*
    * 查询BusinessProjectParameterItemMappingDto两表联查
    * */
    @RequestMapping("/select/BusinessProjectParameterItemMappingAll")
    @ResponseBody
    public List<BusinessProjectParameterItemMappingDto> selectBusinessProjectParameterItemMappingAll(@RequestBody BusinessProjectParameterItemMappingDto projectParameterItemMappingDto) {
        List<BusinessProjectParameterItemMappingDto> parameterItemMappingDtos = parameterItemMappingService.selectAll(projectParameterItemMappingDto);
        return parameterItemMappingDtos;
    }
    /*
     * 查询BusinessProjectParameterItemStandardDataTable表查
     * */
    @RequestMapping("/select/BusinessMatterBasicInfoTableAll")
    @ResponseBody
    public List<BusinessMatterBasicInfoTable> selectBusinessMatterBasicInfoTable(@RequestBody BusinessMatterBasicInfoTable matterBasicInfoTable) {
        List<BusinessMatterBasicInfoTable> standardDataTables = parameterItemMappingService.selectBusinessMatterBasicInfoTable(matterBasicInfoTable);
        return standardDataTables;
    }
    /**
     * 查询BusinessProjectSingleParameterItemCurrentDataTable表查
     * */
    @RequestMapping("/select/ComponentItemBasicInfoTable")
    @ResponseBody
    public List<ComponentItemBasicInfoTable> selectComponentItemBasicInfoTable(@RequestBody ComponentItemBasicInfoTable componentItemBasicInfoTable) {
        List<ComponentItemBasicInfoTable> currentDataTables = parameterItemMappingService.selectComponentItemBasicInfoTable(componentItemBasicInfoTable);
        return currentDataTables;
    }
    @RequestMapping("/select/ParameterItemTableAll")
    @ResponseBody
    public List<ParameterItemTable> selectParameterItemTable(@RequestBody ParameterItemTable parameterItemTable) {
        List<ParameterItemTable> parameterItemTables = parameterItemMappingService.selectParameterItemTable(parameterItemTable);
        return parameterItemTables;
    }
    @RequestMapping("/insert/BusinessProjectParameterItemMapping")
    @ResponseBody
    public Result insertBusinessProjectParameterItemMapping(@RequestBody JSONObject jsonObject) {

        JSONArray dataArray = jsonObject.getJSONArray("requestData");

        BusinessProjectParameterItemMappingMain mainTable = new BusinessProjectParameterItemMappingMain();
        Integer id = parameterItemMappingService.getBusinessProjectParameterItemMappingMainAutoid();
        id = id != null ? ++id : 1;
        int businessProjectId=jsonObject.getInteger("businessProjectID");
        int itemType=jsonObject.getInteger("itemType");
        String description=jsonObject.getString("description");
       Integer status=jsonObject.getInteger("status");
        mainTable.setId(id);
        mainTable.setBusinessProjectId(businessProjectId);
        mainTable.setItemType(itemType);
        mainTable.setDescription(description);
        mainTable.setStatus(status);
        int a = parameterItemMappingService.insertBusinessProjectParameterItemMappingMain(mainTable);
        if (a != 1) {
            return Result.failure("500");
        }
        if (dataArray != null && !dataArray.isEmpty()) {
            List<BusinessProjectParameterItemMappingDto> djBhList = dataArray.toJavaList(BusinessProjectParameterItemMappingDto.class);
            for (BusinessProjectParameterItemMappingDto dataStructureDto : djBhList) {
                BusinessProjectParameterItemMappingSub subTable = new BusinessProjectParameterItemMappingSub();
                BeanUtils.copyProperties(dataStructureDto, subTable);
                Integer autoid = parameterItemMappingService.getBusinessProjectParameterItemMappingSubAutoid();
                autoid = autoid != null ? ++autoid : 1;
                subTable.setId(autoid);
                subTable.setMID(id);
                int b = parameterItemMappingService.insertBusinessProjectParameterItemMappingSub(subTable);
                if (b != 1) {
                    return Result.failure("500");
                }
            }
        }

        return Result.success(200);
    }
    @RequestMapping("/del/BusinessProjectParameterItemMapping")
    @ResponseBody
    public Result delBusinessProjectParameterItemMapping(@RequestBody BusinessProjectParameterItemMappingDto projectParameterItemMappingDto) {
        int id=projectParameterItemMappingDto.getId();
        List<BusinessProjectParameterItemMappingSub> dataStructureDtos=parameterItemMappingService.selectSub(id);
        int c=parameterItemMappingService.delBusinessProjectParameterItemMappingMainStatus(id);
        if (dataStructureDtos.size()>0){
            int d=parameterItemMappingService.delBusinessProjectParameterItemMappingSub(id);
        }
        if (c!=1){
            return Result.failure("500");
        }
        return Result.success(200);
    }
    @RequestMapping("/update/BusinessProjectParameterItemMapping")
    @ResponseBody
    public Result updateBusinessProjectParameterItemMapping(@RequestBody JSONObject jsonObject) {
        JSONArray dataArray = jsonObject.getJSONArray("requestData");
//        log.debug("dataStructureDto.getChineseName="+dataStructureDto.getChineseName());
        BusinessProjectParameterItemMappingMain mainTable = new BusinessProjectParameterItemMappingMain();
        Integer id=jsonObject.getInteger("id");
        int businessProjectId=jsonObject.getInteger("businessProjectID");
        int itemType=jsonObject.getInteger("itemType");
        String description=jsonObject.getString("description");
        int status=jsonObject.getInteger("status");
        mainTable.setId(id);
        mainTable.setBusinessProjectId(businessProjectId);
        mainTable.setItemType(itemType);
        mainTable.setDescription(description);
        mainTable.setStatus(status);
        int a = parameterItemMappingService.updateBusinessProjectParameterItemMappingMain(mainTable);
        if (a != 1) {
            return Result.failure("500","更新主表失败");
        }
        List<BusinessProjectParameterItemMappingSub> dataStructureDtos=parameterItemMappingService.selectSub(id);
        if (dataStructureDtos.size()>0){
            int sub = parameterItemMappingService.delBusinessProjectParameterItemMappingSub(id);
        }
        if (dataArray==null||dataArray.isEmpty()){
            return Result.success(200);
        }
        List<BusinessProjectParameterItemMappingSub> djBhList = dataArray.toJavaList(BusinessProjectParameterItemMappingSub.class);
        for (BusinessProjectParameterItemMappingSub dataStructureDto : djBhList) {
            BusinessProjectParameterItemMappingSub subTable = new BusinessProjectParameterItemMappingSub();
            BeanUtils.copyProperties(dataStructureDto, subTable);
            Integer autoid = parameterItemMappingService.getBusinessProjectParameterItemMappingSubAutoid();
            autoid = autoid != null ? ++autoid : 1;
            subTable.setId(autoid);
            subTable.setMID(id);
            int b = parameterItemMappingService.insertBusinessProjectParameterItemMappingSub(subTable);
            if (b != 1) {
                return Result.failure("500","更新子表失败");
            }
        }
        return Result.success(200);
    }
}
